<?php
Yii::import('zii.widgets.CMenu');
/**
 * A CMenu which collects its items from the installed modules.
 *
 * To use it, simply create a hook file
 * "application.widgets.NavBar.$context.php" and within that file
 * call $event->sender->addItem(), which is NavBar::addItem().
 * 
 * @uses CMenu
 * @version $id$
 * @copyright 2010 &copy; The YAP Group
 * @author Flavius Aspra <flav@yet-another-project.com> 
 * @license License {@link http://yet-another-project.com/project/yap/LICENSE.txt}
 * @todo add the possibility of having different sorting strategies for items
 */
class NavBar extends CMenu {
	/**
	 * The context of the menu
	 *
	 * It can be seen as the name of the menu. The event raised will have the id
	 * "application.widgets.NavBar.$context"
	 * 
	 * @var string
	 * @access public
	 */
	public $context = 'navbar';
	public $tryDefault = TRUE;
	/**
	 * raises the event
	 * "application.widgets.NavBar.$context"
	 * which gives hooks the chance of calling NavBar::addItem() via
	 * $event->sender->addItem().
	 * 
	 * @access public
	 * @return void
	 */
	public function run() {
		$items = $this->items;
		ksort($items);
		$this->items = new SplPriorityQueue;
		foreach($items as $p => $i) {
			$this->items->insert($i,array('x',$p));
		}
		$ev = new ExtEvent($this,$this->tryDefault);
		Yii::app()->modulehook->raiseEvent('application.widgets.NavBar.'.$this->context,$ev);
		parent::run();
	}
	/**
	 * addItem add an item to the navigation bar
	 * 
	 * @param mixed $item 
	 * @param mixed $priority 
	 * @access public
	 * @return void
	 */
	public function addItem($item,$priority) {
		$this->items->insert($item,$priority);
	}
}
